package ano.it.controller;

import ano.it.constant.MessageConstant;
import ano.it.entity.PageResult;
import ano.it.entity.QueryPageBean;
import ano.it.entity.Result;
import ano.it.pojo.Device;
import ano.it.service.DeviceService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Reference
    private DeviceService deviceService;
    @RequestMapping("/add")
    public Result add(@RequestBody Device device){
        try {
            deviceService.add(device);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_DEVICE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_DEVICE_SUCCESS);
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=deviceService.findPage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            deviceService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_DEVICE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_DEVICE_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Device device;
        try {
            device=deviceService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_DEVICE_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_DEVICE_SUCCESS,device);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Device device){
        try {
            deviceService.edit(device);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_DEVICE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_DEVICE_SUCCESS);
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Device> devices=deviceService.findAll();
            return new Result(true, MessageConstant.QUERY_DATA_SUCCESS,devices);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_DATA_FAIL);
        }

    }

}
